package cn.xerllent.wxtp.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;

import cn.hutool.core.util.ObjectUtil;
import cn.xerllent.common.service.impl.BaseServiceImpl;
import cn.xerllent.wxtp.entity.WxCorp;
import cn.xerllent.wxtp.service.WxCorpService;
import cn.xerllent.wxtp.service.mapper.WxCorpMapper;
import cn.xerllent.common.utils.FileUtil;

@Service
//@AllArgsConstructor
//@CacheConfig(cacheNames = "user")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class WxCorpServiceImpl extends BaseServiceImpl<WxCorpMapper, WxCorp>  implements WxCorpService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(WxCorp filter,Pageable pageable) {
    	pageHelper(pageable);
        PageInfo<WxCorp> page = new PageInfo<>(queryAll(filter));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("list", page.getList());
        map.put("total", page.getTotal());
        return map;
    }

    @Override
    //@Cacheable
    public List<WxCorp> queryAll(WxCorp filter) {
    	//方法1
    	QueryWrapper<WxCorp> querywrapper = new QueryWrapper<>();
    	if(ObjectUtil.isNotEmpty(filter)) {
        	if(ObjectUtil.isNotEmpty(filter.getCorpId())) {
        		querywrapper.eq("corp_id", filter.getCorpId());
        	}
        	if(ObjectUtil.isNotEmpty(filter.getCorpName())) {  //模糊查询字段
        		querywrapper.and(w->
        		w.like("corp_name", filter.getCorpName()).or()
        		.like("corp_full_name", filter.getCorpName())
				);
        	}
        	if(ObjectUtil.isNotEmpty(filter.getSubjectType())) {
        		querywrapper.eq("subject_type", filter.getSubjectType());
        	}
        	if(filter.getStatus()!=null) {
        		querywrapper.eq("status", filter.getStatus());
        	}
    	}
    	return baseMapper.selectList(querywrapper);
    }

    @Override
    public void download(List<WxCorp> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (WxCorp wxCorp : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("id", wxCorp.getId());
            map.put("企业id", wxCorp.getCorpId());
            map.put("授权应用id", wxCorp.getAgentId());
            map.put("企业永久授权码", wxCorp.getPermanentCode());
            map.put("企业名称", wxCorp.getCorpName());
            map.put("企业全称", wxCorp.getCorpFullName());
            map.put("企业类型", wxCorp.getSubjectType());
            map.put("企业认证到期时间", wxCorp.getVerifiedEndTime());
            map.put("账户状态", wxCorp.getStatus());
            map.put("创建时间", wxCorp.getCreatetime());
            map.put("变动时间", wxCorp.getUpdatetime());
            //map.put("原始数据包", wxCorp.getWxpackage());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
    
	/**
	 * 清除对应Mapper的二级缓存，采用无害删除空数据模拟清空,因为二级缓存更新机制是本表数据更新即可清空<br>
	 * 提供本方法供联表更新查询的实时更新
	 * */
	@Override
	public void clearCache() {
		baseMapper.deleteById(0);
	}

}
